<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Defray_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function add_defray($data){
        $this->db->insert('defray',$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function count_defray_find($key='',$user_id=''){
        $this->db->select('defray_id');
        $this->db->from('defray');
        $this->db->join('users','defray.user_id = users.user_id','left');
        if($key){
            $this->db->like('users.user_uname', $key);
        }
        if($user_id){
            $this->db->where('defray.user_id', $user_id);
        }
        $this->db->where('defray.delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function get_defray_list($limit,$start,$key,$user_id=''){
        $this->db->select('defray.*,users.user_id,users.user_uname');
        $this->db->from('defray');
        $this->db->join('users','defray.user_id = users.user_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('users.user_uname', $key);
        }
        if($user_id){
            $this->db->where('defray.user_id', $user_id);
        }
        $this->db->where('defray.delete',0);
        $this->db->order_by('defray.defray_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function update_defray($defray_id,$data){
        $this->db->where('defray_id',$defray_id);
        $this->db->update('defray', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_one_defray($defray_id){
        $this->db->select('*');
        $this->db->from('defray');
        $this->db->where('defray_id',$defray_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
    public function count_shouzhi_defray($type=''){
        $this->db->select_sum('defray_money');
        $this->db->from('defray');
        $this->db->where('defray_type',$type);
        $query = $this->db->get();
        $result = $query->result();
        return ($result[0]->defray_money)?$result[0]->defray_money:'0.00';
    }

}
